import { getCategoryCount, getGoodsAmountList } from '@/service/dashboard/dashboard'
import type { ICategoryCountList, IGetGoodsAmountList } from '@/types/dashboard/dashboard'

import { defineStore } from 'pinia'

const useDashboardStore = defineStore('dashboard', {
  state: () => ({
    getGoodsAmountList: [] as IGetGoodsAmountList[],
    categoryCountList: [] as ICategoryCountList[]
  }),
  getters: {
    categoryCountDate(state) {
      return state.categoryCountList.map((item) => {
        return { name: item.name, value: item.goodsCount }
      })
    }
  },
  actions: {
    async getGoodsAmountListAction() {
      // 获取商品总销量
      const getGoodsAmountRes = await getGoodsAmountList()
      this.getGoodsAmountList = getGoodsAmountRes.data

      // 分类商品的数量
      const categoryCountRes = await getCategoryCount()
      this.categoryCountList = categoryCountRes.data
    }
  }
})

export default useDashboardStore
